package org.millioncall.yueyoga.admin.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.millioncall.openplatform.util.StringUtils;
import org.millioncall.yueyoga.admin.model.CashDetail;
import org.millioncall.yueyoga.admin.model.CashModel;
import org.millioncall.yueyoga.admin.service.CashService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CashAction extends BaseAction {

	private CashService cashService;

	public CashService getCashService() {
		return cashService;
	}

	public void setCashService(CashService cashService) {
		this.cashService = cashService;
	}

	/**
	 * 收银日报表
	 * */
	@RequestMapping(value = "/findCash.jhtml")
	public String toCash(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		
		String strStartDate = request.getParameter("startdate");
		String strEndDate = request.getParameter("enddate");
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("hallcode", this.getUser(request).getOrganizationId());
		
		if (!StringUtils.isBlank(strStartDate)) {
			map.put("startdate", strStartDate);
		}
		
		if (!StringUtils.isBlank(strEndDate)) {
			map.put("enddate", strEndDate);
		}
		
		CashModel cm = this.cashService.findCash(map);
		model.put("cashModel", cm);
		model.put("cmCount", cm.getCashReportList().size());
		
		model.put("startdate", strStartDate);
		model.put("enddate", strEndDate);
		
		return "cash_list";
	}
	
	/**
	 * 收银日报表明细
	 * */
	@RequestMapping(value="/findCashByType.jhtml")
	public String toCashDetail(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		
		String type = request.getParameter("type");
		String strStartDate = request.getParameter("startdate");
		String strEndDate = request.getParameter("enddate");
		
		model.put("type", type);
		
		type = translate(type);
		
		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isBlank(strStartDate)) {
			map.put("startdate", strStartDate);
		}
		
		if (!StringUtils.isBlank(strEndDate)) {
			map.put("enddate", strEndDate);
		}
		map.put("type", type);
		map.put("hallcode", this.getUser(request).getOrganizationId());
		List<CashDetail> list = this.cashService.findCashDetailByType(map);
		if (list.size() > 0)
			model.put("cashDetailList", list);
		
		model.put("startdate", strStartDate);
		model.put("enddate", strEndDate);
		
		return "cash_type";
	}
	
	private String translate(String str) {
		String tempStr = "";
	    try {
	      tempStr = new String(str.getBytes("ISO-8859-1"), "utf-8");
	      tempStr = tempStr.trim();
	    }
	    catch (Exception e) {
	      System.err.println(e.getMessage());
	    }
	    return tempStr;
	}
}
